package mtn.bowen.yan.server.controller;

import com.google.common.collect.Lists;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.scene.chart.LineChart;
import javafx.scene.chart.PieChart;
import javafx.scene.control.ListView;
import javafx.scene.image.ImageView;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import mtn.bowen.yan.server.enums.DishesTypeEnum;
import mtn.bowen.yan.server.model.FoodRank;
import mtn.bowen.yan.server.model.PollsSuggest;
import mtn.bowen.yan.server.service.JavaFxUiService;
import mtn.bowen.yan.server.service.PollsSuggestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.net.URL;
import java.util.*;

/**
 * @author <a href=mailto:yanbowen90@foxmail.com> bowen.yan </a>
 * @version 1.0
 * @description: TODO
 * @date 2021-10-21 11:54 PM
 */
@Component
public class FoodRankingController extends BaseController implements Initializable {

    private final static Logger LOGGER = LoggerFactory.getLogger(FoodRankingController.class);

    @FXML
    private LineChart<String, Number> areaChart;

    @FXML
    private PieChart pieChart;

    @Resource
    private PollsSuggestService pollsSuggestService;

    @FXML
    private VBox foodRanding;

    @Resource
    private JavaFxUiService javaFxUiService;

    @SuppressWarnings("unchecked")
    @Override
    public void initialize(URL location, ResourceBundle resources) {
        init();

        Map<Integer, List<PollsSuggest>> pollsNumByDay = pollsSuggestService.getPollsNumByDay(7);
        //构建折线图
        pollsSuggestService.createLineChart(areaChart, pollsNumByDay);
        //构建饼图
        pollsSuggestService.createPieChart(pieChart);
    }

    private void init() {
        Text pollsTitle = new Text();
        pollsTitle.setText("本月最受欢迎菜品排名");
        pollsTitle.setFont(Font.font("KaiTi", FontWeight.NORMAL, 30));
        foodRanding.setSpacing(25.0);
        foodRanding.getChildren().add(pollsTitle);

        Map<String, FoodRank> foodRankMap = pollsSuggestService.selectPollsInfo(30);
        List<FoodRank> list = Lists.newArrayList();
        foodRankMap.forEach((key, value) -> {
            list.add(value);
        });
        // 降序排序
        Collections.sort(list);
        int flag = 9;
        if (list.size() < flag) {
            flag = list.size();
        }

        HBox pollsAllData = new HBox();
        pollsAllData.setAlignment(Pos.CENTER);
        pollsAllData.setSpacing(35.0);

        //left
        VBox leftVBox = new VBox();
        for (int count = 1; count <= flag; count++) {
            HBox hBox = new HBox();
            hBox.setSpacing(60.0);
            hBox.setPadding(new Insets(6, 10, 6, 10));
            hBox.setAlignment(Pos.CENTER_LEFT);

            ImageView view = javaFxUiService.getImageView(32.0, 32.0, "/icons/" + count + "_round_solid.png");
            Text name = javaFxUiService.getText("菜品名称：" + list.get(count - 1).getName(), Font.font("KaiTi", FontWeight.NORMAL, 22));

            hBox.getChildren().add(view);
            hBox.getChildren().add(name);
            leftVBox.getChildren().add(hBox);
        }

        //middle
        VBox middleVBox = new VBox();
        for (int count = 1; count <= flag; count++) {
            HBox hBox = new HBox();
            hBox.setSpacing(30.0);
            hBox.setPadding(new Insets(6, 10, 6, 10));
            hBox.setAlignment(Pos.CENTER);

            Text chef = javaFxUiService.getText("大厨：" + list.get(count - 1).getChef(), Font.font("KaiTi", FontWeight.NORMAL, 22));
            ImageView view = javaFxUiService.getImageView(32.0, 32.0, "/icons/" + count + "_round_solid.png");
            view.setVisible(false);

            hBox.getChildren().add(chef);
            hBox.getChildren().add(view);
            middleVBox.getChildren().add(hBox);
        }

        //right
        VBox rightVBox = new VBox();
        for (int count = 1; count <= flag; count++) {
            HBox hBox = new HBox();
            hBox.setSpacing(20.0);
            hBox.setPadding(new Insets(6, 10, 6, 10));
            hBox.setAlignment(Pos.CENTER);

            Text polls = javaFxUiService.getText("点赞数： " + list.get(count - 1).getCount().toString(), Font.font("KaiTi", FontWeight.NORMAL, 22));
            ImageView view = javaFxUiService.getImageView(32.0, 32.0, "/icons/" + count + "_round_solid.png");
            view.setVisible(false);

            hBox.getChildren().add(polls);
            hBox.getChildren().add(view);
            rightVBox.getChildren().add(hBox);
        }

        pollsAllData.getChildren().addAll(leftVBox, middleVBox, rightVBox);
        foodRanding.getChildren().add(pollsAllData);
    }

}
